####################################################################################
###
###   Immigration, Public Housing and Support for the French National Front
###   Gloria Gennaro
###
###   Paper Table A9, A10
###
####################################################################################


library(miceadds)


################################################################################
# Set Up
################################################################################

# Load Working Directories
source(paste0(wd_main, '/2_code/00_working_directories.R'))

# Load data and clean
bdw = 3500
source(paste0(wd_code, '/04_data_load_clean_didsc.R'))


################################################################################
# Key new variables
################################################################################

# Post treatment are elections starting with 2002
df_did$post = ifelse(df_did$policy_period>=0, 1, 0)

# this variable is defined previously, as the difference between the legal population threshold and the relevant threshold
df_did$treat = ifelse(df_did$running>0, 1, 0)


################################################################################
# Table A9
################################################################################

print("- TABLE A9 -")

# Column 1: main 
print("-------------- Column 1 --------------")
mod = lm(fn ~ running*treat*post, data=df_did)
mod_cl = coeftest(mod, vcov = vcovCL(mod, cluster=df_did$CODGEO))

  # Print
  print(round(mod_cl[c('treat:post'), 1:2], 3))
  print(paste0("Observations: ", attr(mod_cl, 'nobs')))
  print(paste0("R-squared: ", round(summary(mod)$r.squared, 3)))


# Column 2: HTE
print("-------------- Column 2 --------------")
mod = lm(fn ~ running*treat*post*imm_quant_90, data=df_did)
mod_cl = coeftest(mod, vcov = vcovCL(mod, cluster=df_did$CODGEO))

  # Print
  print(round(mod_cl[c('treat:post', 'treat:post:imm_quant_902', 'treat:post:imm_quant_903'), 1:2], 3))
  print(paste0("Observations: ", attr(mod_cl, 'nobs')))
  print(paste0("R-squared: ", round(summary(mod)$r.squared, 3)))
  
  
# Column 3: Controls
print("-------------- Column 3 --------------")
mod = lm(fn ~ running*treat*post*imm_quant_90 + running*treat*post*res_quant + 
              running*treat*post*hlm_quant + running*treat*post*empty_quant, 
              data=df_did)
mod_cl = coeftest(mod, vcov = vcovCL(mod, cluster=df_did$CODGEO))

  # Print
  print(round(mod_cl[c('treat:post', 'treat:post:imm_quant_902', 'treat:post:imm_quant_903'), 1:2], 3))
  print(paste0("Observations: ", attr(mod_cl, 'nobs')))
  print(paste0("R-squared: ", round(summary(mod)$r.squared, 3)))

  
# Column 4: Controls + time FE
print("-------------- Column 4 --------------")
mod = lm(fn ~ running*treat*post*imm_quant_90 + running*treat*post*res_quant + 
              running*treat*post*hlm_quant + running*treat*post*empty_quant + as.factor(policy_period), 
              data=df_did)
mod_cl = coeftest(mod, vcov = vcovCL(mod, cluster=df_did$CODGEO))

  # Print
  print(round(mod_cl[c('treat:post', 'treat:post:imm_quant_902', 'treat:post:imm_quant_903'), 1:2], 3))
  print(paste0("Observations: ", attr(mod_cl, 'nobs')))
  print(paste0("R-squared: ", round(summary(mod)$r.squared, 3)))

  
# Column 5: time FE + region FE + Controls
print("-------------- Column 5 --------------")
mod = lm(fn ~ running*treat*post*imm_quant_90 + as.factor(policy_period) + as.factor(reg) + 
              running*treat*post*res_quant + running*treat*post*hlm_quant + running*treat*post*empty_quant, 
              data=df_did)
mod_cl = coeftest(mod, vcov = vcovCL(mod, cluster=df_did$CODGEO))

  # Print
  print(round(mod_cl[c('treat:post', 'treat:post:imm_quant_902', 'treat:post:imm_quant_903'), 1:2], 3))
  print(paste0("Observations: ", attr(mod_cl, 'nobs')))
  print(paste0("R-squared: ", round(summary(mod)$r.squared, 3)))

  

################################################################################
# Table A10
################################################################################

print("- TABLE A10 -")
  
# Loop over different bandwidths
bdws = c(3500, 3000, 2500, 2000, 1500, 1000, 950, 900)

for (b in bdws){

  print(paste0("-------------- Bandwidth: ", b, " --------------"))
  
  temp = df_did[which(abs(df_did$running)<=b),]
  
  mod = lm(fn ~ running*treat*post*imm_quant_90 + as.factor(policy_period) + as.factor(reg) + 
             running*treat*post*res_quant + running*treat*post*hlm_quant + running*treat*post*empty_quant, 
           data=temp)
  mod_cl = coeftest(mod, vcov = vcovCL(mod, cluster=temp$CODGEO))
  
  # Print
  print(round(mod_cl[c('treat:post', 'treat:post:imm_quant_902', 'treat:post:imm_quant_903'), 1:2], 3))
  print(paste0("Observations: ", attr(mod_cl, 'nobs')))
  print(paste0("R-squared: ", round(summary(mod)$r.squared, 3)))
  
}


